<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="设备ID" prop="deviceId">
        <el-input v-model="formData.deviceId" placeholder="请输入设备ID" />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="formData.deviceName" placeholder="请输入设备名称" />
      </el-form-item>
      <el-form-item label="设备唯一标识符，全局唯一，用于识别设备" prop="deviceKey">
        <el-input v-model="formData.deviceKey" placeholder="请输入设备唯一标识符，全局唯一，用于识别设备" />
      </el-form-item>
      <el-form-item label="网关ID" prop="gatewayId">
        <el-input v-model="formData.gatewayId" placeholder="请输入网关ID" />
      </el-form-item>
      <el-form-item label="日产流量" prop="lisDay">
        <el-input v-model="formData.lisDay" placeholder="请输入日产流量" />
      </el-form-item>
      <el-form-item label="全部流量" prop="lisAll">
        <el-input v-model="formData.lisAll" placeholder="请输入全部流量" />
      </el-form-item>
      <el-form-item label="年产水量" prop="lisYear">
        <el-input v-model="formData.lisYear" placeholder="请输入年产水量" />
      </el-form-item>
      <el-form-item label="月产流量" prop="listMonth">
        <el-input v-model="formData.listMonth" placeholder="请输入月产流量" />
      </el-form-item>
      <el-form-item label="时间" prop="lisTime">
        <el-date-picker
          v-model="formData.lisTime"
          type="date"
          value-format="x"
          placeholder="选择时间"
        />
      </el-form-item>
      <el-form-item label="产水流量" prop="lisSun">
        <el-input v-model="formData.lisSun" placeholder="请输入产水流量" />
      </el-form-item>
      <el-form-item label="产水累积量" prop="lisSunAll">
        <el-input v-model="formData.lisSunAll" placeholder="请输入产水累积量" />
      </el-form-item>
      <el-form-item label="产水接收时间" prop="lisSunTime">
        <el-date-picker
          v-model="formData.lisSunTime"
          type="date"
          value-format="x"
          placeholder="选择产水接收时间"
        />
      </el-form-item>
      <el-form-item label="小时流量" prop="lisSunHour">
        <el-input v-model="formData.lisSunHour" placeholder="请输入小时流量" />
      </el-form-item>
      <el-form-item label="小时流量开始" prop="hourBegin">
        <el-input v-model="formData.hourBegin" placeholder="请输入小时流量开始" />
      </el-form-item>
      <el-form-item label="小时流量结束" prop="hourEnd">
        <el-input v-model="formData.hourEnd" placeholder="请输入小时流量结束" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DeviceLatestFlowLogApi, DeviceLatestFlowLogVO } from '@/api/mqtt/devicelatestflowlog'

/** 水量日志 表单 */
defineOptions({ name: 'DeviceLatestFlowLogForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  deviceId: undefined,
  deviceName: undefined,
  deviceKey: undefined,
  gatewayId: undefined,
  lisDay: undefined,
  lisAll: undefined,
  lisYear: undefined,
  listMonth: undefined,
  lisTime: undefined,
  lisSun: undefined,
  lisSunAll: undefined,
  lisSunTime: undefined,
  lisSunHour: undefined,
  hourBegin: undefined,
  hourEnd: undefined,
})
const formRules = reactive({
  deviceId: [{ required: true, message: '设备ID不能为空', trigger: 'blur' }],
  deviceName: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
  deviceKey: [{ required: true, message: '设备唯一标识符，全局唯一，用于识别设备不能为空', trigger: 'blur' }],
  gatewayId: [{ required: true, message: '网关ID不能为空', trigger: 'blur' }],
  lisDay: [{ required: true, message: '日产流量不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DeviceLatestFlowLogApi.getDeviceLatestFlowLog(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DeviceLatestFlowLogVO
    if (formType.value === 'create') {
      await DeviceLatestFlowLogApi.createDeviceLatestFlowLog(data)
      message.success(t('common.createSuccess'))
    } else {
      await DeviceLatestFlowLogApi.updateDeviceLatestFlowLog(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    deviceId: undefined,
    deviceName: undefined,
    deviceKey: undefined,
    gatewayId: undefined,
    lisDay: undefined,
    lisAll: undefined,
    lisYear: undefined,
    listMonth: undefined,
    lisTime: undefined,
    lisSun: undefined,
    lisSunAll: undefined,
    lisSunTime: undefined,
    lisSunHour: undefined,
    hourBegin: undefined,
    hourEnd: undefined,
  }
  formRef.value?.resetFields()
}
</script>